﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Phoenix.AppCode.Entities;
using System.Data.SqlClient;
using System.Data;

namespace Phoenix.AppCode.Data
{
    public class MenuListDAO : BaseDAO
    {
        #region Backend method
        /// <summary>
        /// Insert MenuList
        /// </summary>
        /// <param name="menulist"></param>
        public void Create(MenuList menulist)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spMenuListCreate", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@URL", menulist.URL));
                command.Parameters.Add(new SqlParameter("@ID_MenuList_Parent", menulist.ID_MenuList_Parent));
                command.Parameters.Add(new SqlParameter("@MenuBack_FrontEnd", menulist.MenuBack_FrontEnd));
                command.Parameters.Add(new SqlParameter("@Location", menulist.Location));
                command.Parameters.Add(new SqlParameter("@MenuName_vi", menulist.MenuName_vi));
                command.Parameters.Add(new SqlParameter("@MenuName_en", menulist.MenuName_en));

                connection.Open();
                command.ExecuteNonQuery();
                //if (paramId.Value != DBNull.Value)
                //MenuList.ID = (long)paramId.Value;
                //else
                //  throw new DataAccessException(String.Format(Resources.Strings.CreateEntityException, ""));
            }
        }

        /// <summary>
        /// Edit MenuList 
        /// </summary>
        /// <param name=""></param>
        public void Edit(MenuList menulist)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spMenuListEdit", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_MenuList", menulist.ID_MenuList));
                command.Parameters.Add(new SqlParameter("@URL", menulist.URL));
                command.Parameters.Add(new SqlParameter("@ID_MenuList_Parent", menulist.ID_MenuList_Parent));
                command.Parameters.Add(new SqlParameter("@MenuBack_FrontEnd", menulist.MenuBack_FrontEnd));
                command.Parameters.Add(new SqlParameter("@Location", menulist.Location));
                command.Parameters.Add(new SqlParameter("@MenuName_vi", menulist.MenuName_vi));
                command.Parameters.Add(new SqlParameter("@MenuName_en", menulist.MenuName_en));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.UpdateEntityException, "", .ID));
            }
        }

        /// <summary>
        /// On/Off view
        /// </summary>
        /// <param name="menulist"></param>
        public void OnOff(MenuList menulist)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spMenuListOnOff", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_MenuList", menulist.ID_MenuList));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.UpdateEntityException, "", .ID));
            }
        }


        /// <summary>
        /// Delete MenuList 
        /// </summary>
        /// <param name=""></param>
        public void Delete(MenuList menulist)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spMenuListDelete", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_MenuList", menulist.ID_MenuList));

                connection.Open();
                command.ExecuteNonQuery();
                //if (command.ExecuteNonQuery() <= 0)
                //    throw new DataAccessException(String.Format(Resources.Strings.DeleteEntityException, "", .ID));
            }
        }

        /// <summary>
        /// Show MenuList  All
        /// </summary>
        /// <returns></returns>
        //public DataTable MenuListList(String language)
        //{
        //    using (SqlConnection connection = GetConnection())
        //    {
        //        SqlCommand command = new SqlCommand("spMenuListList", connection);
        //        command.CommandType = CommandType.StoredProcedure;
        //        command.Parameters.Add("@Language", SqlDbType.NVarChar).Value = language;
        //        SqlDataAdapter da = new SqlDataAdapter(command);

        //        connection.Open();
        //        DataTable dt = new DataTable();
        //        da.Fill(dt);

        //        return dt;
        //    }
        //}

        /// <summary>
        /// Show MenuList by BackEnd - FrontEnd
        /// </summary>
        /// <returns></returns>
        public DataTable MenuListListByBeFe(int menubackfrontend, String language)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spMenuListListByBeFe", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add("@MenuBack_FrontEnd", SqlDbType.Int).Value = menubackfrontend;
                command.Parameters.Add("@Language", SqlDbType.NVarChar).Value = language;
                SqlDataAdapter da = new SqlDataAdapter(command);

                connection.Open();
                DataTable dt = new DataTable();
                da.Fill(dt);

                return dt;
            }
        }

        /// <summary>
        /// Show data by ID
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public DataTable MenuListEdit_GetByID(int idmenulist)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spMenuListEdit_GetByID", connection);
                command.CommandType = CommandType.StoredProcedure;
                command.Parameters.Add("@ID_MenuList", SqlDbType.Int).Value = idmenulist;
                SqlDataAdapter da = new SqlDataAdapter(command);

                connection.Open();
                DataTable dt = new DataTable();
                da.Fill(dt);

                return dt;
            }
        }

        /// <summary>
        /// Get ID to Edit or Delete
        /// </summary>
        /// <param name="idmenulist"></param>
        /// <returns></returns>
        public MenuList GetMenuListByID(int idmenulist)
        {
            using (SqlConnection connection = GetConnection())
            {
                SqlCommand command = new SqlCommand("spMenuListEdit_GetByID", connection);
                command.CommandType = CommandType.StoredProcedure;

                command.Parameters.Add(new SqlParameter("@ID_MenuList", idmenulist));

                connection.Open();
                MenuList menulist = null;
                using (IDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    if (reader.Read())
                        menulist = CreateMenuListFromReader(reader);
                    //else
                    //   throw new DataAccessException(String.Format(Resources.Strings.GetEntityException, "", ID));
                }

                return menulist;
            }
        }

        private MenuList CreateMenuListFromReader(IDataReader reader)
        {
            MenuList menulist = new MenuList();
            menulist.ID_MenuList = (int)reader["ID_MenuList"];
            menulist.MenuName = (string)reader["MenuName"];
            menulist.URL = (string)reader["URL"];
            menulist.ID_MenuList_Parent = (int)reader["ID_MenuList_Parent"];
            menulist.MenuBack_FrontEnd = (int)reader["MenuBack_FrontEnd"];
            menulist.Location = (int)reader["Location"];
            menulist.Visible = (Boolean)reader["Visible"];
            menulist.Language = (string)reader["Language"];
            return menulist;
        }
        #endregion
    }
}